package vg.modules.search.components;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Set;

import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;

import vg.modules.search.FBSearchPanel;
import vg.modules.search.SearchPluginGlobals;
import vg.modules.search.components.additionalAttributesPanel.AdditionalAttributePanel;
import vg.modules.search.components.searchTreePanel.SearchTree;
import vg.services.data_base_manager.data.skeleton.GraphTreeElement;
import vg.services.plugin_manager.realization.PluginParameter;

public class SecondPanel implements FBSecondPanel {
	// Swing components
	private final JPanel view;
	private final JSplitPane searchTreeAndAAPanelSplitPane;
	// Main data
	private final PluginParameter parameter;
	private int actionMode = SearchPluginGlobals.DEF_ACTION_MODE_1;
	// Second panel's components
	private final SearchTree searchTree;
	private final BrowseModePanel brawseModePanel; 
	private final ActionModePanel actionModePanel;
	private final AdditionalAttributePanel aaPanel;
	/**
	 * Constructor.
	 * @param parameter - connection with model and user interface.
	 * @param searchPanel - feedback with search panel.
	 */
	public SecondPanel(final PluginParameter parameter, final FBSearchPanel fbSearchPanel) {
		this.parameter = parameter;
		this.view = new JPanel(new GridBagLayout());
		//---------------------------------------
		this.aaPanel = new AdditionalAttributePanel();
		this.searchTree = new SearchTree(this.parameter, this);
		this.brawseModePanel = new BrowseModePanel(this);
		this.actionModePanel = new ActionModePanel(this);
		//create split pane between conditional panel and additional panel
		this.searchTreeAndAAPanelSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, this.searchTree.getView(), this.aaPanel.getView());
		this.searchTreeAndAAPanelSplitPane.setResizeWeight(0.5);
		this.searchTreeAndAAPanelSplitPane.setDividerSize(4);
		//build search panel
		GridBagConstraints gbc = new GridBagConstraints(0,0,  1,1,  1,1,  GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5),  0,0);
		this.view.add(this.searchTreeAndAAPanelSplitPane, gbc);

		gbc = new GridBagConstraints(0,1,  1,1,  0,0,  GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5),  0,0);
		this.view.add(this.brawseModePanel.getView(), gbc);
		
		gbc = new GridBagConstraints(0,2,  1,1,  0,0,  GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5),  0,0);
		this.view.add(this.actionModePanel.getView(), gbc);				
	}
	/**
	 * This method builds tree and additional attribute panel.
	 */
	public synchronized void buildTree(GraphTreeElement rootNode, Set<Integer> vertexIdList, Set<String> vertexAttributes) {
		this.searchTree.replaceNode(rootNode, vertexIdList);
		this.aaPanel.setVertexAttributes(vertexAttributes);
	}
	public JPanel getView() {
		return(this.view);
	}
	/**
	 * This method updates user interface.
	 */
	public synchronized void updateUITheme() {
		SwingUtilities.updateComponentTreeUI(this.view);
	}
	///////////////////////////////////////////////////////////////////////////
	// IMPLEMENTATION OF FBSECONDPANEL
	///////////////////////////////////////////////////////////////////////////
	public synchronized void setActionMode(int mode) {
		this.actionMode = mode;
	}
	public synchronized void setBrowseMode(int mode) {
		this.searchTree.setBrowseMode(mode);
	}
	public synchronized int getActionMode() {
		return(this.actionMode);
	}
	public synchronized Set<String> getVertexAdditionalAttributes() {
		return(this.aaPanel.getVertexAdditionalAttributes());
	}
}
